//220121

var http = require('http');
var fs = require('fs');
var url = require('url');

var port = 8888;
http.createServer(function(req, resp) {
	var pathname = url.parse(req.url).pathname;
	
	console.log("Request for " + pathname + " received.");
	
	// 相对路径
	pathname = '22M01/' + pathname;
	
	// 从文件系统中读取请求的文件内容
	fs.readFile(pathname, function (err, data) {
		if (err) {
			console.log(err);
			
			// HTTP 状态码: 404 : NOT FOUND
			// Content Type: text/html
			resp.writeHead(404, {'Content-Type': 'text/html; charset=utf8'});
		} else {
			// HTTP 状态码: 200 : OK
			resp.writeHead(200, {'Content-Type': 'text/html; charset=utf8'});
			
			resp.write(data.toString());
		}
		
		resp.end();
	});
}).listen(port);

console.log('Server running at http://127.0.0.1:' + port);
